package com.zh.note.huawei.onehundred.比赛的冠亚季军;

import java.util.*;
import java.util.function.ToIntFunction;

/**
 * https://dream.blog.csdn.net/article/details/131276768
 */
public class Main {
    static class Sportsman implements Comparable<Sportsman> {
        int id;
        int power;

        public Sportsman(int id, int power) {
            this.id = id;
            this.power = power;
        }

        @Override
        public int compareTo(Sportsman o) {
            // power相同，id小的获胜
            return Comparator.comparingInt(new ToIntFunction<Sportsman>() {
                        @Override
                        public int applyAsInt(Sportsman p) {
                            return p.power;
                        }
                    }).reversed()
                    .thenComparing(p -> p.id)
                    .compare(this, o);

        }
    }

    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String input = sc.nextLine();
        String input = "2 3 4 5 6";
        String[] s = input.split(" ");
        List<Sportsman> list = new ArrayList<>();
        for (int i = 0; i < s.length; i++) {
            Sportsman sman = new Sportsman(i, Integer.valueOf(s[i]));
            list.add(sman);
        }
        List<Sportsman> win = new ArrayList<>();
        List<Sportsman> lose = new ArrayList<>();
        solution(list, win, lose);
        //对于自然排序，一个类需要实现Comparable并定义compareTo方法。
        win.sort(Comparator.naturalOrder());
        lose.sort(Comparator.naturalOrder());
        System.out.println(win.get(0).id + " " + win.get(1).id + " " + lose.get(0).id);


    }

    private static void solution(List<Sportsman> dataList, List<Sportsman> win, List<Sportsman> lose) {
        //保存下一轮要比较的数据
        List<Sportsman> nextList = new ArrayList<>();
        int size = dataList.size();
        Iterator<Sportsman> iterator = dataList.iterator();
        while (iterator.hasNext()) {
            Sportsman one = iterator.next();
            if (!iterator.hasNext()) { //后面没有元素了
                if (size == 3) {
                    win.add(one);
                } else {
                    // 放到下一轮
                    nextList.add(one);
                }
                break;
            }
            //有下一个元素
            Sportsman two = iterator.next();
            if (one.power >= two.power) {
                if (size <= 4) {
                    win.add(one);
                    lose.add(two);
                } else {
                    // 放到下一轮
                    nextList.add(one);
                }
            } else {
                // 第一个元素小于第二个元素
                if (size <= 4) {
                    win.add(two);
                    lose.add(one);
                } else {
                    // 放到下一轮
                    nextList.add(two);
                }
            }
        }
        if (!nextList.isEmpty()) {
            solution(nextList, win, lose);
        }
    }
}
